package com.tencreat.mall.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.DateUtils;
import com.tencreat.common.utils.StringUtils;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.domain.mall.MallCoupon;
import com.tencreat.service.mall.IMallCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;

/**
 * 优惠券信息及规则Controller
 *
 * @author tencreat
 * @date 2021-08-15
 */
@Api(tags="优惠券信息及规则api")
@RestController
@RequestMapping("/mall/coupon")
public class AdminMallCouponController extends BaseController {
    @Autowired
    private IMallCouponService mallCouponService;

    /**
     * 查询优惠券信息及规则列表
     */
    @ApiOperation(value="查询优惠券信息及规则列表")
    @PreAuthorize("@ss.hasPermi('mall:coupon:list')")
    @PostMapping("/list")
    public HttpResult<IPage<MallCoupon>> list(@RequestBody  MallCoupon mallCoupon) {
        QueryWrapper<MallCoupon> wrapper = Wrappers.query(mallCoupon);
        if (StringUtils.isNotEmpty(mallCoupon.getStartTimeStr())  && StringUtils.isNotEmpty(mallCoupon.getEndTimeStr())){
            wrapper.between("end_time",mallCoupon.getStartTimeStr(),mallCoupon.getEndTimeStr());
        }
        if (mallCoupon.getSortStr() != null && mallCoupon.getSortDesc() != null){
            if (Objects.equals(mallCoupon.getSortDesc(), "ascending")){
                wrapper.orderByAsc(mallCoupon.getSortStr());
            }
            wrapper.orderByDesc(mallCoupon.getSortStr());
        }else {
            wrapper.orderByDesc("create_time");
        }
        Page<MallCoupon> page =  new Page<>(mallCoupon.getPageNum(), mallCoupon.getPageSize());
        return HttpResult.ok(mallCouponService.page(page, wrapper));
    }

    /**
     * 导出优惠券信息及规则列表
     */
    @ApiOperation(value="导出优惠券信息及规则列表")
    @PreAuthorize("@ss.hasPermi('mall:coupon:export')")
    @Log(title = "营销配置>优惠券管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(MallCoupon mallCoupon) {
        List<MallCoupon> list = mallCouponService.selectMallCouponList(mallCoupon);
        ExcelUtil<MallCoupon> util = new ExcelUtil<MallCoupon>(MallCoupon.class);
        return util.exportExcel(list, "coupon");
    }

    /**
     * 获取优惠券信息及规则详细信息
     */
    @ApiOperation(value="获取优惠券信息及规则详细信息")
    @PreAuthorize("@ss.hasPermi('mall:coupon:query')")
    @PostMapping(value = "/getInfo")
    public HttpResult<MallCoupon> getInfo(@RequestBody   ReqIdParam id) {
        return HttpResult.ok(mallCouponService.selectMallCouponById(id));
    }

    /**
     * 新增优惠券信息及规则
     */
    @ApiOperation(value="新增优惠券信息及规则")
    @PreAuthorize("@ss.hasPermi('mall:coupon:add')")
    @Log(title = "营销管理>优惠券管理", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public HttpResult<Boolean> add(@RequestBody  MallCoupon mallCoupon) {
        if(StringUtils.isEmpty(mallCoupon.getStartTimeStr())){
            return HttpResult.error("活动时间必须设置");
        }
        if(StringUtils.isBlank(mallCoupon.getName())){
            return HttpResult.error("优惠券名称不能为空");
        }
        DateTimeFormatter df = DateTimeFormatter.ofPattern(DateUtils.YYYY_MM_DD_HH_MM_SS);
        mallCoupon.setStartTime(LocalDateTime.parse(mallCoupon.getStartTimeStr(),df));
        mallCoupon.setEndTime(LocalDateTime.parse(mallCoupon.getEndTimeStr(),df));

        if(mallCoupon.getStartTime().isBefore(LocalDateTime.of(LocalDate.now(), LocalTime.MIN))){
            return HttpResult.error("开始时间不能小于当前时间");
        }

        return to(mallCouponService.insertMallCoupon(mallCoupon,getLoginCheck()));
    }

    /**
     * 修改优惠券信息及规则
     */
    @ApiOperation(value="修改优惠券信息及规则")
    @PreAuthorize("@ss.hasPermi('mall:coupon:edit')")
    @Log(title = "营销管理>优惠券管理", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<Boolean> edit(@RequestBody  MallCoupon mallCoupon) {
        return to(mallCouponService.updateMallCoupon(mallCoupon));
    }

    /**
     * 删除优惠券信息及规则
     */
    @ApiOperation(value="删除优惠券信息及规则")
    @PreAuthorize("@ss.hasPermi('mall:coupon:remove')")
    @Log(title = "营销管理>优惠券管理", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody  ReqIdsParam ids) {
        return to(mallCouponService.deleteMallCouponByIds(ids));
    }
}
